/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#pragma once
#include "button.hpp"
#include "drawnode.hpp"
#include<vector>

namespace MFUI{
	class Canvas:public Button{
		typedef void(*DrawCallback)(Canvas*,SDL_FPoint*);
		//默认回调
		static void pointcallback(Canvas*,SDL_FPoint*);
		protected:
		~Canvas()override;
		float w,h;
		SDL_Texture *texture,*imagecache;
		bool painted;//每次交互或apply时触发 逻辑更新结束绘制时重置
		bool build,clean;
		void* pixels;

		public:
		std::vector<DrawNode*> commands;
		SDL_Color defaultcolor;
		Uint8 alpha;
		DrawCallback callback;
		bool visible;
		//需要调用applychange w h为画布尺寸 与控件无关
		Canvas(Window* win,int w,int h);
		void draw(Renderer* r,bool updatecache)override;
		void mousemove(Window* w,SDL_FPoint p)override;
		bool confirmdraw()override;

		void setcolor(SDL_Color color);
		void drawfpoint(SDL_FPoint p);
		void drawpoint(SDL_Point p);
		void drawfrect(SDL_FRect& r);
		void fillfrect(SDL_FRect& r);
		void drawrect(SDL_Rect& r);
		void fillrect(SDL_Rect& r);
		void drawftexture(SDL_Texture* t,SDL_Rect& src,SDL_FRect& dst,Uint8 a=255);
		void drawtexture(SDL_Texture* t,SDL_Rect& src,SDL_Rect& dst,Uint8 a=255);
		void drawftexturex(SDL_Texture* t,SDL_Rect& src,SDL_FRect& dst,
		SDL_RendererFlip f,double& degree,SDL_FPoint& center,Uint8 a=255);
		void drawtexturex(SDL_Texture* t,SDL_Rect& src,SDL_Rect& dst,
		SDL_RendererFlip f,double& degree,SDL_Point& center,Uint8 a=255);

		void applychange();
		//通知像素缓存更新请求
		void applybuild(bool clean);
		//清空已有指令，不影响已build缓存的
		void clear();
		//添加到窗口后才分配
		void* getpixels()const;
		SDL_FPoint getcanvassize()const;

		//有缓存的绘制指令时才调用
		virtual void buildfinish();
	};
}